package main

import (
	"io"
	"io/ioutil"
	"log"
	"net"
)

func demo() {
	conn, err := net.Dial("tcp", "time-track.cn:80")
	assertErr(err)

	_, err = conn.Write([]byte("HEAD / HTTP/1.0\r\n\r\n"))
	assertErr(err)

	//reads from r until an error or EOF
	result, err := ioutil.ReadAll(conn)
	assertErr(err)

	println(string(result))
}

func main() {
	l, err := net.Listen("tcp", ":2000")    //:2000 host省略，表示本地ip
	defer l.Close()
	assertErr(err)

	for {
		conn, err := l.Accept()
		assertErr(err)

		go func(c net.Conn) {
			io.Copy(c, c)
			c.Close()
		}(conn)
	}
}

func assertErr(err error) {
	if err != nil {
		log.Fatal(err)
		panic(err)
	}
}
